
package fileby.gui.misc;

import com.google.inject.Inject;
import fileby.core.ActionKey;
import fileby.ctrl.actions.IActionProvider;
import fileby.util.properties.IPropertyBundleProvider;
import fileby.util.properties.PropertyBundle;
import java.awt.Font;
import javax.swing.Action;


/**
 *
 * @author Daniel
 */
public class SearchBox extends javax.swing.JPanel {

    private String location;
    private IActionProvider actionProvider;
    private IPropertyBundleProvider propertyProvider;

    @Inject
    public SearchBox(IActionProvider actionProvider,
            IPropertyBundleProvider propertyProvider) {

        this.propertyProvider = propertyProvider;
        this.actionProvider = actionProvider;
        initComponents();
        initCtrl();
    }

    public void setText(String s) {
        location = s;
        searchBox.setFont(searchBox.getFont().deriveFont(Font.ITALIC));
        searchBox.setText("Search in " + location);
        searchBox.setCaretPosition(0);
    }

    private void initCtrl() {
        Action search = actionProvider.getControl(ActionKey.SEARCH);
        searchButton.setAction(search);
        searchBox.setAction(search);
        search.putValue(ActionKey.SEARCH.toString(), searchBox);
        
        //Have to set icons after action, otherwise the action replaces them
        PropertyBundle bundle = propertyProvider.create(this.getClass());
        searchButton.setIcon(bundle.getIcon("searchButton.icon"));
        searchButton.setRolloverIcon(bundle.getIcon("searchButton.pressedIcon"));
        searchButton.setPressedIcon(bundle.getIcon("searchButton.pressedIcon"));
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="This used to be Auto-Generated Code">
    private void initComponents() {

        searchBox = new javax.swing.JTextField();
        searchButton = new javax.swing.JButton();

        setOpaque(false);

        PropertyBundle bundle = propertyProvider.create(this.getClass());
        searchBox.setFont(bundle.getFont("searchBox.font")); // NOI18N
        searchBox.setBorder(javax.swing.BorderFactory.createEtchedBorder(bundle.getColor("searchBox.border.highlightColor"),
                bundle.getColor("searchBox.border.shadowColor"))); // NOI18N
        
        searchButton.setBackground(bundle.getColor("searchButton.background")); // NOI18N
        searchButton.setBorder(null);
        searchButton.setBorderPainted(false);
        searchButton.setContentAreaFilled(false);
        searchButton.setFocusPainted(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(searchBox, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(searchButton, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(searchBox, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
            .addComponent(searchButton, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
        );
    }// </editor-fold>
    
    private javax.swing.JTextField searchBox;
    private javax.swing.JButton searchButton;


}
